<?php
/**
 *CommentServices
 * @author tan bing
 * @date 2021-11-15 9:39
 */


namespace Modules\Order\Services\Api;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Modules\Order\Entities\Order;
use Modules\Order\Entities\OrderComment;
use Modules\Order\Entities\OrderDetail;

class CommentServices extends BaseService
{

    /**
     * @var Request
     * @author tan bing
     */
    private Request $request;

    /**
     * CommentServices constructor.
     * @param Request $request
     * @author tan bing
     * @date 2021-11-15 11:08
     */
    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * 订单评论预览页面
     * @return array
     * @throws \Exception
     * @author tan bing
     * @date 2021-11-15 9:56
     */
    public function commentPreview()
    {
        $orderId = $this->request->input('order_id');
        $type = $this->request->input('type');
        $userId = $this->request->attributes->get('userId');
        $where = [
            'is_delete' => 0,
            'user_id'   => $userId,
            'id'         => $orderId,
        ];
        $order = Order::query()->where($where)->first();
        if(!$order) throw new \Exception('订单不存在');
        if($order->order_status != 3)
            throw new \Exception('订单尚未收货，无法评价');
        if($order->order_status == 6)
            throw new \Exception('订单已评价');

        switch ($type) {
            case 'mall' :
            $order_detail_list = OrderDetail::query()->where(['order_id' => $orderId, 'is_delete' => 0])->select('id as order_detail_id', 'goods_id', 'goods_pic')->get();
                break;
            default:
                //TODO 后续新增积分商城订单
                $order_detail_list = [];
                break;
        }

        return [
            'order_id' => $order->id,
            'goods_list' => $order_detail_list,
        ];
    }

    /**
     * 订单评价
     * @return array|string
     * @throws \Throwable
     * @author tan bing
     * @date 2021-11-15 10:51
     */
    public function comment()
    {
        $type = $this->request->post('type');
        $orderId = $this->request->post('order_id');
        $goodsList = $this->request->post('goods_list');
        $goodsList = json_decode($goodsList, true);
        $userId = $this->request->attributes->get('userId');
        $order = Order::query()->where(['id' => $orderId, 'user_id' => $userId, 'is_delete' => 0])->first();
        if(!$order)
            throw new \Exception('订单不存在或已删除');

        if (!$goodsList)
            throw new \Exception('商品信息不能为空');

        DB::beginTransaction();
        try {

            foreach ($goodsList as $goods) {
                $orderDetail = OrderDetail::query()->where(['id' => $goods['order_detail_id'], 'order_id' => $order->id, 'goods_id' => $goods['goods_id'], 'is_delete' => 0])->first();
                if(!$orderDetail) continue;

                $orderComment = new OrderComment();
//                $orderComment->store_id = 0;
                $orderComment->order_detail_id = $orderDetail->id;
                $orderComment->user_id = $userId;
                $orderComment->order_id = $orderId;
                $orderComment->goods_id = $orderDetail->goods_id;
                $orderComment->score = $goods['score'];
                $orderComment->content = $goods['content'];
                //$order_comment->content = mb_convert_encoding($order_comment->content, 'UTF-8');
                $orderComment->content = preg_replace('/[\xf0-\xf7].{3}/', '', $orderComment->content);
                $orderComment->pic_list = json_encode($goods['uploaded_pic_list'], JSON_UNESCAPED_UNICODE);
                if (!$orderComment->save()) {
                    throw new \Exception('评价上传失败');
                }
            }
            $order->order_status = 6;
            if(!$order->save()) throw new \Exception('订单评价失败');

            DB::commit();
        } catch (\Exception $e) {
            DB::rollBack();
            throw new \Exception($e->getMessage());
        }

        return $type;
    }
}